﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace System.Web
{
    //System.Web
    //WebApplicationTestJs.Models
    public class HttpResponseStream2 : Stream
    {

        public HttpResponseStream2(HttpWriter writer)
        {
            this._writer = writer;
        }

        public override bool CanRead
        {
            get
            {
                return false;
            }
        }
        public override bool CanSeek
        {
            get
            {
                return false;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }

        public override long Length
        {
            get
            {
                throw new NotSupportedException();
            }
        }

        public override long Position
        {
            get
            {
                throw new NotSupportedException();
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    this._writer.Close();
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }

        public override void Flush()
        {
            this._writer.Flush();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotSupportedException();
        }

        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            //if (this._writer.IgnoringFurtherWrites)
            //{
            //    return;
            //}
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            if (buffer.Length - offset < count)
            {
                //獲取數據超限，count太大
                throw new ArgumentException("獲取數據超限，count太大");
                //throw new ArgumentException(SR.GetString("InvalidOffsetOrCount", new object[]
                //{
                //    "offset",
                //    "count" 
                //}));
            }
            if (count == 0)
            {
                return;
            }
            //this._writer.WriteFromStream(buffer, offset, count);
        }

        private HttpWriter _writer;
    }
}